package com.atguigu.controller;

import com.atguigu.pojo.Article;
import com.atguigu.pojo.Result;
import com.atguigu.service.ArticleService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping("add")
    public Result add(@RequestBody Article article) {
        articleService.add(article);
        System.out.println("添加的数据是  "+article);
        return Result.success();
    }
    @PutMapping("update")
    public Result update(@RequestBody Article article) {

        System.out.println("---------------------");
        System.out.println(article);
        System.out.println("---------------------");

        articleService.update(article);
        return Result.success();
    }

    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        articleService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        articleService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("selectAll")
    public Result selectAll(Article article) {
        List<Article> list = articleService.selectAll(article);
        return Result.success(list);
    }

    @GetMapping("selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        return Result.success(articleService.selectById(id));
    }

    @GetMapping("selectByOne")
    public Result selectByOne(@RequestParam Integer id) {
        return Result.success(articleService.selectById(id));
    }

    @GetMapping("selectPage")
    public Result selectPage(Article article,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(articleService.selectPage(article, pageNum, pageSize));
    }

}
